'use strict';
const db = uniCloud.database()
exports.main = async (event, context) => {
	const collection = db.collection('hexueuser')
	let res =null;
	console.log(event)
	switch (event.action) {
		case 'lookVideo':
		{
			console.log(event.data)
			for(let k of event.data){
				let sout=await uniCloud.httpclient.request(
					 "https://api.jinkex.com/edu/v1/classroom/"+k.course_id+"/"+
					k.open_id+"/"+k.id+"?token="+
					event.token+"&school_host=haut.hnscen.cn",{
						contentType: 'json',
					method:"GET",
					dataType: 'json'
				})
				if(sout.data.data.resource.progress!="1.000"){
					for(let r of sout.data.data.resource.resourceList){
						await uniCloud.httpclient.request(
							"https://api.jinkex.com/edu/v1/classroom/"+k.course_id+"/"+
							k.open_id+"/resource/"+k.id,
							{method:"POST",
							contentType: 'json',
							dataType: 'json',
							data:{
								token:event.token,
								school_host:"haut.hnscen.cn",
								resourceId:r.id
							}
						})
						console.log("下载文件",r)
					}
				}
				let  timeout=setInterval(()=>{
					uniCloud.httpclient.request(
					"https://api.jinkex.com/edu/v1/classroom/"+k.course_id+"/"+
					k.open_id+"/rec/"+k.id+"?token="+
					event.token+"&school_host=haut.hnscen.cn",{method:"POST",
					contentType: 'json',
					data:{
						token:event.token,
						school_host:"haut.hnscen.cn",
						lastTime:60,
						hasTime:60
					},
					dataType: 'json'}).then(res=>{
						if(res.data.data.progress==1){
							console.log("结束观看")
							clearInterval(timeout);
						}
					})
				},60*1000)
			}
			break;
		}
		case 'ziliao':
		{
			for(let k of event.data){
				let sout=await uniCloud.httpclient.request(
					 "https://api.jinkex.com/edu/v1/classroom/"+k.course_id+"/"+
					k.open_id+"/"+k.id+"?token="+
					event.token+"&school_host=haut.hnscen.cn",{
						contentType: 'json',
					method:"GET",
					dataType: 'json'
				})
				if(sout.data.data.resource.progress!="1.000"){
					for(let r of sout.data.data.resource.resourceList){
						await uniCloud.httpclient.request(
							"https://api.jinkex.com/edu/v1/classroom/"+k.course_id+"/"+
							k.open_id+"/resource/"+k.id,
							{method:"POST",
							contentType: 'json',
							dataType: 'json',
							data:{
								token:event.token,
								school_host:"haut.hnscen.cn",
								resourceId:r.id
							}
						})
						console.log("下载文件",r)
					}
				}
			}
			break;
		}
		case '666':
			{
				for(let k of event.data){
					let sout=await uniCloud.httpclient.request(
						 "https://api.jinkex.com/edu/v1/classroom/"+k.course_id+"/"+
						k.open_id+"/"+k.id+"?token="+
						event.token+"&school_host=haut.hnscen.cn",{
							contentType: 'json',
						method:"GET",
						dataType: 'json'
					})
					console.log(sout)
					while (1) {
						let res=await uniCloud.httpclient.request(
						"https://api.jinkex.com/edu/v1/classroom/"+k.course_id+"/"+
						k.open_id+"/rec/"+k.id+"?token="+
						event.token+"&school_host=haut.hnscen.cn",{method:"POST",
						contentType: 'json',
						data:{
							token:event.token,
							school_host:"haut.hnscen.cn",
							lastTime:60,
							hasTime:60
						},
						dataType: 'json'})
						console.log(res)
						if(res.data.data.progress==1){
							break;
						}
					}
				}
				break;
			}
		case 'answer':
			{
				console.log(event.data)
				for(let k of event.data){
					let res=await uniCloud.httpclient.request(
					"https://api.jinkex.com/edu/v1/classroom/"+k.course_id+"/"+
					k.open_id+"/"+k.id+"?token="+
					event.token+"&school_host=haut.hnscen.cn",{method:"GET",
					contentType: 'json',
					dataType: 'json'})
					let kdc=[];
					for(let aw of res.data.data.praxise.praxiseList){
						//拼接答案
						kdc.push({praxiseId:aw.id,value:aw.answer||(aw.type=='muti'?"[1]":"1")})
					}
					//提交答案
					let kdcnb=await uniCloud.httpclient.request(
					"https://api.jinkex.com/edu/v1/classroom/"+k.course_id+"/"+k.open_id+"/submit/"+k.id,{
					method:"POST",
					data:{
						token:event.token,
						school_host:"haut.hnscen.cn",
						items:JSON.stringify(kdc)
					},
					contentType: 'json',
					dataType: 'json'})
				}
				for(let k of event.data){
					let res=await uniCloud.httpclient.request(
					"https://api.jinkex.com/edu/v1/classroom/"+k.course_id+"/"+
					k.open_id+"/"+k.id+"?token="+
					event.token+"&school_host=haut.hnscen.cn",{method:"GET",
					contentType: 'json',
					dataType: 'json'})
					let kdc=[];
					for(let aw of res.data.data.praxise.praxiseList){
						kdc.push({praxiseId:aw.id,value:aw.answer||(aw.type=='muti'?"[1]":"1")})
					}
					let kdcnb=await uniCloud.httpclient.request(
					"https://api.jinkex.com/edu/v1/classroom/"+k.course_id+"/"+k.open_id+"/submit/"+k.id,{
					method:"POST",
					data:{
						token:event.token,
						school_host:"haut.hnscen.cn",
						items:JSON.stringify(kdc)
					},
					contentType: 'json',
					dataType: 'json'})
					console.log("考试完成")
				}
				break;
			}
		case 'getCourse':
			{
				let kdcnb=[]
				let dati=[]
				let course=await uniCloud.httpclient.request(
				"https://api.jinkex.com/edu/v1/studentcourse/getstuentcourse?token="+
				event.data.token+"&school_host=haut.hnscen.cn",{method:"GET",
				contentType: 'json',
				dataType: 'json'})
				course=course.data.data[0].course
				for(let courseitem of course){
					let courselist= await uniCloud.httpclient.request(
					"https://api.jinkex.com/edu/v1/classroom/"+courseitem.course_id+"/"+
					courseitem.open_id+"?token="+
					event.data.token+"&school_host=haut.hnscen.cn",{method:"GET",
					contentType: 'json',
					dataType: 'json'})
					courselist.data.data.chapter.forEach(videolist=>{
						videolist.children.forEach(video=>{
							uniCloud.httpclient.request(
							"https://api.jinkex.com/edu/v1/classroom/"+courseitem.course_id+"/"+
							courseitem.open_id+"/"+video.id+
							"?token="+event.data.token+"&school_host=haut.hnscen.cn",{method:"GET",
							contentType: 'json',
							dataType: 'json'}).then(res=>{
								if(res.data.data.resource.hasCount!=0&&res.data.data.resource.progress!="1.000"){
									res.data.data.resource.resourceList.forEach(r=>{
										uniCloud.httpclient.request(
										"https://api.jinkex.com/edu/v1/classroom/"+courseitem.course_id+"/"+
										courseitem.open_id+"/resource/"+video.id,{method:"POST",
										contentType: 'json',
										data:{
											token:event.data.token,
											school_host:"haut.hnscen.cn",
											resourceId:r.id
										},
										dataType: 'json'}).then(re=>{
											console.log("下载文件",re)
										})
									})
								}
							})
							if(video.videoid==""){
								if(video.rec.praxise_submit_count!=2){
									dati.push({
										course_id:courseitem.course_id,
										open_id:courseitem.open_id,
										id:video.id
									})
								}
							}else if(video.rec.progress!="1.000"){
								kdcnb.push({
									course_id:courseitem.course_id,
									open_id:courseitem.open_id,
									id:video.id
								})
							}
						})
					})
				}
				res={videoList:kdcnb,subjectList:dati}
				break;
			}
		case 'login':
			{
				let kdc =await collection.where({id:event.data.id}).get()
				if(kdc.data.length>0){
					console.log("已注册")
				}else{
					await collection.add(event.data);
				}
				break;
			}
		case 'logout':
			res = await uniID.logout(event.uniIdToken);
			break;
	}
	return res
};